﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class gamemanager : MonoBehaviour
{
    // h主界面
    // r继续或重新开始
    // p暂停 
    public int isGame = 1;

    public GameObject player;
    public GameObject core;
    public GameObject lose;
    public GameObject victory;

    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.Find("blueQuilt");
        core = GameObject.Find("good").transform.Find("core").gameObject;
        lose = GameObject.Find("UI").transform.Find("lose").gameObject;
        victory = GameObject.Find("UI").transform.Find("victor").gameObject;
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKey(KeyCode.H))
        {
            // 回到主界面
            isGame = 0;
            goHome();
        }

        if(Input.GetKey(KeyCode.P) && (isGame == 1))
        {
            // 暂停游戏
            pause();
        }

        if(Input.GetKey(KeyCode.R) && (isGame == 1))
        {
            // 继续游戏
            goon();

        }

        if(Input.GetKey(KeyCode.R) && (isGame == 0))
        {
            // 重新开始游戏
            replay();
        }

        //这里不能单纯的用这个比较 
        if(player != null && (core == null))
        {
            isGame = 0;
            // 暂停游戏，并显示胜利UI
            Time.timeScale = 0;
            victory.SetActive(true);
        }
        else
        {
            if(player == null && (core != null))
            {
                isGame = 0;
                // 暂停游戏，并显示失败UI
                Time.timeScale = 0;
                lose.SetActive(true);
            }
        }
    }

    void goHome()
    {
        SceneManager.LoadScene(1);
    }

    void pause()
    {
        Time.timeScale = 0;
    }

    void goon()
    {
        Time.timeScale = 1;
    }

    void replay()
    {
        Time.timeScale = 1;
        SceneManager.LoadScene(2);
    }
}
